package de.lns.tick;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;


public class Controller {
	/**Make the constructor invisible.
	 * Since this is a static-access class,
	 * it shouldn't be instantiated.
	 */
	private Controller(){};
	
	//Status variables
	private static int mouseX;
	private static int mouseY;
	private static int moveX;
	private static int moveY;
	
	protected static void initialize() {
		try {
			Mouse.create();
			Keyboard.create();
		} catch (LWJGLException e) {
			UpdateThread.kill("Error while initializing Mouse/Keyboard", e);
		}
		Mouse.setClipMouseCoordinatesToWindow(true);
		Keyboard.enableRepeatEvents(false);
	}
	
	protected static void update() {
		while(Mouse.next()){
			MouseEvent m = new MouseEvent();
			m.action = Mouse.getEventButton();
			m.pressed = Mouse.getEventButtonState();
			m.mouseX = Mouse.getEventX();
			m.mouseY = Mouse.getEventY();
			m.scroll = Mouse.getEventDWheel();
			State.active().mouse(m);
		}
		
		while(Keyboard.next()){
			KeyEvent k = new KeyEvent();
			k.keyCode = Keyboard.getEventKey();
			k.key = Keyboard.getEventCharacter();
			k.pressed = Keyboard.getEventKeyState();
			k.repeat = Keyboard.isRepeatEvent();
			State.active().key(k);
		}
		
		Mouse.poll();
		mouseX = Mouse.getX();
		mouseY = Mouse.getY();
		moveX = Mouse.getDX();
		moveY = Mouse.getDY();
	}
	
	public static int getMouseX(){
		return mouseX;
	}
	
	public static int getMouseY(){
		return mouseY;
	}
	
	public static int getMoveX(){
		return moveX;
	}
	
	public static int getMoveY(){
		return moveY;
	}
	
}
